<template>
  <div id="app">
    <el-form ref="addGoodsForm" :model="g" label-width="80px" :rules="ruleForm">
      <el-form-item label="商品名称" prop="goodsName">
        <el-input v-model="g.goodsName"></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="g.price" type="number"></el-input>
      </el-form-item>
      <el-form-item label="商品介绍" prop="description">
        <el-input v-model="g.description"></el-input>
      </el-form-item>
      <el-form-item label="生产日期" prop="createTime">
        <el-date-picker v-model="g.createTime"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="请选择生产日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="addGoods">添加商品信息</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  name: "AddGoodsForm",
  data(){
    return{
      g:{},
      ruleForm:{
        goodsName: [{required: true, message: "商品名称不能为空", trigger: "blur"}],
        price: [{required: true, message: "商品价格不能为空", trigger: "blur"}],
        description: [{required: true, message: "商品描述不能为空", trigger: "blur"}],
        createTime: [{required: true, message: "生产日期不能为空", trigger: "blur"}]
      }
    }
  },
  methods:{
    addGoods(){
      this.$refs["addGoodsForm"].validate(valid=>{
        if (valid){
          this.axios.post("/GoodsManager/goods/",this.g)
            .then(resp=>{
              if(resp.data.code==200&&resp.data.message=="success"){
                alert("添加成功");
                this.$router.push("/goods/show")
              }else {
                alert("添加失败");
              }
            })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
